﻿using _17bangMVC.Models;
using SRV.ServiceInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _17bangMVC.Controllers
{
    public class ProfileController : Controller
    {
        private IUserService userService;
        public ProfileController(IUserService userService)
        {
            this.userService = userService;
        }
        // GET: Profile
        [HttpGet]
        public ActionResult Write()
        {
            ProfileModel model = userService.GetProfile();
            return View(model);
        }
        [HttpPost]
        public ActionResult Write(ProfileModel model, HttpPostedFileBase icon)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            if (icon.ContentLength > 1024 * 1024)
            {
                ModelState.AddModelError("icon", "文件太大了");
                return View();
            }
            if (icon.ContentType != "image/png")
            {
                ModelState.AddModelError("icon", "文件类型错误");
                return View();
            }
            DateTime now = DateTime.Now;

            string urlPath = $"\\UpLoadFiles\\{now.Year}\\{now.Month}\\{now.Day}";
            Guid guid = Guid.NewGuid();
            string extension = Path.GetExtension(icon.FileName);
            string urlName = $"{urlPath}\\{guid}{extension}";

            model.Icon = urlName;

            Directory.CreateDirectory(Server.MapPath(urlPath));//创建文件
            icon.SaveAs(Server.MapPath(urlName));//保存上传的图片文件
            return View();


        }
    }
}